<?php
/**
 * 商品分类模型
 * @author maniac
 */
namespace app\common\model;

class GoodCategory extends SoftDel
{
    protected $table = 'maniac_good_category';
    public $tableName = '商品分类';         // 数据表名称
    public $logId     = ['id', 'name'];     // 日志操作相关标识
    public $subModel  = 'Goods';            // 子模型名称
    public $subId     = 'category_id';      // 子模型操作相关标识
    
    protected $autoWriteTimestamp = true;   // 自动写入时间戳
    
    // 自动转换
    protected $type = [
        'list_order' => 'integer',
    ];
    
    /**
     * 父级栏目名称获取器
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getSuperCategoriesNameAttr($value, $data)
    {
        if ($data['super_category_ids']) {
            $where   = [
                ['id', 'in', $data['super_category_ids']],
            ];
            $categories = GoodCategory::where($where)->column('name');
            return implode('<font>></font>', $categories).'<font>></font>';
        }
        return '';
    }
    
    /**
     * 图片地址获取器
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getImageUriAttr($value, $data)
    {
        return $data['image_id'] ? attach_uri($data['image_id']) : '';
    }
    
    /**
     * 广告图片地址获取器
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getAdUriAttr($value, $data)
    {
        return $data['ad_id'] ? attach_uri($data['ad_id']) : '';
    }

    public function getParentNameAttr($value,$data){
        $parent_name="";
        if($data["parent_id"]) {
            $where = [
                ['id', 'in', $data["parent_id"]],
            ];
            $parent_name = GoodCategory::where($where)->value('name');
        }
        return $parent_name;
    }

    public function getGoodsListAttr($value,$data){

        $category_id=[];
        $category_id[]=$data["id"];
        $category_mod=new \app\common\service\GoodCategory();
        $where=[
            ["is_show","=",1],
            ["parent_id","=",$data["id"]]
        ];
        $child=$category_mod->categories($where);
        foreach ($child as $k=>$v){
            $category_id[]=$v["id"];
        }
        $where=[
            ["category_id","IN",$category_id],
            ["is_sale","=",1]
        ];
        $limit=6;
		$field = 'id,goods_name,sale_price,goods_thumb_id';
		$append = ['total_sale_nums','image_uri','activity_price'];
        $goods_list=\app\facade\Goods::goods($where,$limit,$field,'',$append);
        return $goods_list;
    }
}
